package com.xf.tourismapp.fragments;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xf.tourismapp.R;

import android.widget.ListView;
import android.widget.Toast;

import com.xf.tourismapp.activities.SpotDetailActivity;
import com.xf.tourismapp.adapter.SpotAdapter;
import com.xf.tourismapp.constant.Config;
import com.xf.tourismapp.utils.OkHttpUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;



/**
 * 主界面-首页的Fragment
 * (1, "所有类别"));
 * (2, "宗教文化"));
 * (2, "名称古迹"));
 * (2, "自然景点"));
 * (2, "红色旅游"));
 * (2, "历史景点"));
 */
public class HomePageFragment extends Fragment {
    private static final  String TAG="HomePageDebugInfo";
    //景点列表
    private ListView spotList;
    private SpotAdapter spotAdapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_homepage, container, false);
        //初始化View
        initView(view);
        //初始化数据
        //initData();
        return view;
    }

    @SuppressWarnings("unchecked")
    public void initView(View view) {
        spotList =  view.findViewById(R.id.spot_listview);
        spotList.setOnItemClickListener((arg0, arg1, arg2, arg3) -> {
            HashMap<String, Object> map = (HashMap<String, Object>) arg0.getItemAtPosition(arg2);
            Intent intent = new Intent(getActivity(), SpotDetailActivity.class);
            intent.putExtra("map", map);
            getActivity().startActivity(intent);
        });
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        accessNet(Config.URL_GetSpot);
    }

    //从服务器获取所有景点的方法
    public void accessNet(String url)
    {
        List<HashMap<String, Object>> list = new ArrayList<>();
        //String params = "";
        OkHttpUtils HttpUtil=OkHttpUtils.getInstance();
        HttpUtil.doGet(url, new OkHttpUtils.OkHttpCallBackLinener() {
            @Override
            public void failure(Exception e) {
                Toast.makeText(getActivity(),R.string.homepage_net_info, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void success(String json) {
                try {
                    JSONObject jsonObject = new JSONObject(json);
                    int ret = jsonObject.getInt("ret");
                    if (ret == 0) {
                        JSONObject dataObject = jsonObject.getJSONObject("data");
                        int totalNum = dataObject.getInt("totalNum");
                        if (totalNum > 0) {
                            JSONArray spotList = dataObject.getJSONArray("spotsInfo");
                            for (int i = 0; i < spotList.length(); i++) {
                                JSONObject spotMap = (JSONObject) spotList.opt(i);
                                HashMap<String, Object> map = new HashMap<>();
                                map.put("ID", spotMap.get("ID"));
                                map.put("position", spotMap.get("position"));
                                map.put("tour_project", spotMap.get("tour_project"));
                                map.put("price", spotMap.get("price"));
                                map.put("introduction", spotMap.get("introduction"));
                                map.put("picture", spotMap.get("picture"));
                                map.put("time", spotMap.get("time"));
                                list.add(map);
                            }
                        }
                    }
                    spotAdapter = new SpotAdapter(getActivity(), list);
                    spotList.setAdapter(spotAdapter);
                } catch (Exception e) {
                    Log.i(TAG,"JSON解析异常");
                }
            }
        });
    }
}